package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.entity.DbEmpContract;
import org.jeecg.modules.entity.DbFile;
import org.jeecg.modules.entity.DbSalary;
import org.jeecg.modules.mapper.DbSalaryMapper;
import org.jeecg.modules.service.IDbFileService;
import org.jeecg.modules.service.IDbSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: db_salary
 * @Author: jeecg-boot
 * @Date:   2024-07-08
 * @Version: V1.0
 */
@Service
public class DbSalaryServiceImpl extends ServiceImpl<DbSalaryMapper, DbSalary> implements IDbSalaryService {
    @Autowired
    private IDbFileService dbFileService;
    /**
     *   添加
     *
     * @param dbSalary
     * @return
     */
    @Transactional
    public void add(DbSalary dbSalary) {
        updateFileGroupId(dbSalary);
    }
    //    添加附件id
    private void updateFileGroupId(DbSalary dbSalary) {
        if (dbSalary.getFileIds() == null || dbSalary.getFileIds().isEmpty()) {
            this.save(dbSalary);
        } else {
            dbSalary.setFileGroupId(null);
            this.save(dbSalary);
            UpdateWrapper<DbSalary> dbSalaryUpdateWrapper = new UpdateWrapper<>();
            String fileGroupId = "db_salary" + dbSalary.getId();
            dbSalaryUpdateWrapper.set("file_group_id", fileGroupId).eq("id", dbSalary.getId());
            this.update(dbSalary, dbSalaryUpdateWrapper);
            UpdateWrapper<DbFile> updateWrapper = new UpdateWrapper<>();
            DbFile dbFile = new DbFile();
            updateWrapper.set("group_id", fileGroupId).in("id", dbSalary.getFileIds());
            dbFileService.update(dbFile, updateWrapper);
        }
    }
}
